
package com.gamatechno.solodestinationnew.helper;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.gamatechno.solodestinationnew.entity.E_Event;
import com.gamatechno.solodestinationnew.entity.E_Forecast;
import com.gamatechno.solodestinationnew.entity.E_Gallery;
import com.gamatechno.solodestinationnew.entity.E_Komentar;
import com.gamatechno.solodestinationnew.entity.E_Location;
import com.gamatechno.solodestinationnew.utils.Utils;
import com.gamatechno.solodestinationnew.utils.Webservice;
import com.google.android.gms.maps.model.LatLng;

public class JSONHelper {

    public static List<E_Location> searchLocation(JSONArray arr, LatLng location) {
        List<E_Location> listLocation = new ArrayList<E_Location>();
        try {
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj = arr.getJSONObject(i);
                E_Location loc = new E_Location();
                loc.setId(obj.getInt("id"));
                loc.setName(obj.getString("name"));
                loc.setLat(obj.getDouble("lat"));
                loc.setLng(obj.getDouble("lng"));
                loc.setRate((float) obj.getDouble("rating"));
                loc.setKategori(obj.getString("category"));
                double jarak = Utils.hitungJarak(location,
                        new LatLng(loc.getLat(), loc.getLng()));
                jarak = jarak / 1000;
                Utils.TRACE("JSONHelper", "getAllLocation jaraknya : " + jarak);
                loc.setJarak(jarak);
                listLocation.add(loc);
            }
        } catch (JSONException e) {

        }

        Utils.sortingLocation(listLocation);

        return listLocation;
    }

    public static List<E_Location> getAllLocation(JSONArray arr) {
        List<E_Location> listLocation = new ArrayList<E_Location>();
        try {
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj = arr.getJSONObject(i);
                E_Location loc = new E_Location();
                loc.setId(obj.getInt("id"));
                loc.setName(obj.getString("name"));
                // if (obj.has("description"))
                // loc.setDesc(obj.getString("description"));
                // if(obj.has("lat"))
                //
                //
                // if (!TextUtils.isEmpty(obj.getString("lat")))
                // loc.setLat(obj.getDouble("lat"));
                // if (!TextUtils.isEmpty(obj.getString("lng")))
                // loc.setLng(obj.getDouble("lng"));
                // if (!TextUtils.isEmpty(obj.getString("images"))) {
                // if (obj.getString("category").equals("Kuliner")) {
                // loc.setImgThumb(Webservice.GET_IMG_DIR(MainActivity.CATEGORY_KULINER,
                // obj.getString("images")));
                // } else if (obj.getString("category").equals("Hotel")) {
                // loc.setImgThumb(Webservice.GET_IMG_DIR(MainActivity.CATEGORY_HOTEL,
                // obj.getString("images")));
                // } else if (obj.getString("category").equals("Kerajinan")) {
                // loc.setImgThumb(Webservice.GET_IMG_DIR(MainActivity.CATEGORY_HANDYCRAFT,
                // obj.getString("images")));
                // } else if (obj.getString("category").equals("Shopping")) {
                // loc.setImgThumb(Webservice.GET_IMG_DIR(MainActivity.CATEGORY_SHOPPING,
                // obj.getString("images")));
                // } else if (obj.getString("category").equals("Wisata")) {
                // loc.setImgThumb(Webservice.GET_IMG_DIR(MainActivity.CATEGORY_TOURISM,
                // obj.getString("images")));
                // }
                // }
                //
                // loc.setRate((float) obj.getDouble("rating"));
                // loc.setComment(obj.getInt("comment"));

                // hitung jarak
                // loc.setJarak(Utils.getRoundTwoDecimal(Utils.hitungJarak(location,
                // new LatLng(loc.getLat(),
                // loc.getLng())) / 1000));
                listLocation.add(loc);
            }
        } catch (JSONException e) {
            Utils.TRACE("JSONHelper", "getAllLocation : " + e.getMessage());
        }

        // Utils.sortingLocation(listLocation);

        return listLocation;

    }

    /**
     * parse all location by category
     * 
     * @param arr
     * @param category
     * @return
     */
    public static List<E_Location> getAllLocation(JSONArray arr, int category, LatLng location) {
        List<E_Location> listLocation = new ArrayList<E_Location>();
        try {
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj = arr.getJSONObject(i);
                E_Location loc = new E_Location();
                loc.setId(obj.getInt("id"));
                loc.setName(obj.getString("name"));
                loc.setDesc(obj.getString("description"));

                if (!TextUtils.isEmpty(obj.getString("lat")))
                    loc.setLat(obj.getDouble("lat"));
                if (!TextUtils.isEmpty(obj.getString("lng")))
                    loc.setLng(obj.getDouble("lng"));
                if (!TextUtils.isEmpty(obj.getString("images")))
                    loc.setImgThumb(Webservice.GET_IMG_DIR(category, obj.getString("images")));

                loc.setRate((float) obj.getDouble("rating"));
                loc.setComment(obj.getInt("comment"));

                // hitung jarak

                double jarak = Utils.hitungJarak(location,
                        new LatLng(loc.getLat(), loc.getLng()));

                jarak = jarak / 1000;
                Utils.TRACE("JSONHelper", "getAllLocation jaraknya : " + jarak);
                loc.setJarak(jarak);
                listLocation.add(loc);
            }
        } catch (JSONException e) {
            Utils.TRACE("JSONHelper", "getAllLocation : " + e.getMessage());
        }

        Utils.sortingLocation(listLocation);

        return listLocation;
    }

    /**
     * get list forecast
     * 
     * @param arr JSONArray of list from forecast webservice
     * @return
     */
    public static List<E_Forecast> getForecast(JSONArray arr) {
        List<E_Forecast> listForecast = new ArrayList<E_Forecast>();
        try {
            for (int i = 0; i < 3; i++) {
                JSONObject objList = arr.getJSONObject(i);

                E_Forecast forecast = new E_Forecast();
                // converting timestamp into human readable data
                forecast.setDate(Utils.convertTimestampToDate(objList.getLong("dt"), "h:mm a"));
                JSONObject objMain = objList.getJSONObject("main");
                forecast.setTemp(objMain.getInt("temp"));
                JSONArray arrWeather = objList.getJSONArray("weather");
                JSONObject objWeather = arrWeather.getJSONObject(0);
                forecast.setStatus(objWeather.getString("main"));
                forecast.setIcon(Webservice.GET_ICON_PATH_WEATHER(objWeather.getString("icon")));

                listForecast.add(forecast);
            }
        } catch (JSONException e) {
            Utils.TRACE("JSONHelper", "getForecast : " + e.getMessage());
        }
        return listForecast;
    }

    /**
     * get gallery
     * 
     * @param arr
     * @return
     */
    public static List<E_Gallery> getGallery(JSONArray arr) {
        List<E_Gallery> listGallery = new ArrayList<E_Gallery>();
        try {
            for (int i = 0; i < arr.length(); i++) {
                JSONObject objResult = arr.getJSONObject(i);
                E_Gallery gallery = new E_Gallery();
                gallery.setId(objResult.getInt("gallery_id"));
                gallery.setPhoto(objResult.getString("gallery_photo"));
                gallery.setTotalComment(objResult.getInt("totalComment"));
                gallery.setDate(objResult.getString("gallery_date"));
                gallery.setUploadBy(objResult.getString("name"));
                listGallery.add(gallery);
            }
        } catch (JSONException e) {
            Utils.TRACE("JSONHelper", "getGallery : " + e.getMessage());
        }
        return listGallery;
    }

    /**
     * @param arr
     * @return
     */
    public static List<E_Komentar> getListKomentar(JSONArray arr) {
        List<E_Komentar> listKomentar = new ArrayList<E_Komentar>();
        try {
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj = arr.getJSONObject(i);
                E_Komentar komentar = new E_Komentar();
                if (!obj.isNull("user_id"))
                    komentar.setIduser(obj.getString("user_id"));
                if (!obj.isNull("name"))
                    komentar.setName(obj.getString("name"));
                else
                    komentar.setName("User");

                komentar.setRating((float) obj.getDouble("rating"));
                komentar.setKomentar(obj.getString("comment"));
                komentar.setDate(Utils.convertTimestampToDate(obj.getLong("date"),
                        "d MMM yy, h:mm a"));

                listKomentar.add(komentar);
            }
        } catch (JSONException e) {
            Utils.TRACE("JSONHelper", "getListKomentar : " + e.getMessage());
        }
        return listKomentar;
    }

    /*
     * getEvent by @_hakz_
     */
    public static List<E_Event> getEvent(JSONArray arr) {
        List<E_Event> listEvent = new ArrayList<E_Event>();
        try {
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj = arr.getJSONObject(i);
                E_Event ev = new E_Event();
                ev.setId(obj.getInt("id"));
                ev.setName(obj.getString("name"));
                ev.setDescription(obj.getString("description"));
                ev.setLat(obj.getString("lat"));
                ev.setLon(obj.getString("lng"));
                ev.setStartDate(obj.getString("startDate"));
                ev.setEndDate(obj.getString("endDate"));
                ev.setImages(obj.getString("images"));
                ev.setCategory(obj.getString("category"));
                ev.setDistrict_id(obj.getInt("district_id"));
                ev.setDistrict(obj.getString("district"));

                listEvent.add(ev);
            }
        } catch (Exception e) {
            Utils.TRACE("JSONHelper", "getEvent : " + e.getMessage());
        }

        return listEvent;

    }

    public static List<E_Komentar> getListKomentarPhoto(JSONArray arr) {
        List<E_Komentar> listKomentar = new ArrayList<E_Komentar>();
        try {
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj = arr.getJSONObject(i);
                E_Komentar komentar = new E_Komentar();
                komentar.setIduser(obj.getString("user_id"));
                komentar.setName(obj.getString("name"));
                komentar.setKomentar(obj.getString("user_comment"));
                komentar.setDate(Utils.convertTimestampToDate(obj.getLong("date_comment"),
                        "d MMM yy, h:mm a"));
                listKomentar.add(komentar);
            }
        } catch (JSONException e) {
            Utils.TRACE("JSONHelper", "getListKomentarPhoto : " + e.getMessage());
        }
        return listKomentar;
    }
}
